
package com.studiofortress.sf.util;

import java.awt.Polygon;

/**
 * This is a Polygon that allows the user to flip and swap the points along it's
 * axis. Flipping occures along the 0 line on the axis, so if you flip x it
 * becomes -x.
 *
 * @author Joseph Lenton
 */
public class FlipPolygon extends Polygon
{
    /**
     * Creates a new empty FlipPolygon.
     */
    public FlipPolygon()
    {
        super();
    }

    /**
     * Same as the longer FlipPolygon constructor only it uses the length of
     * either xpoints or ypoints as the number of points. For a valid polygon
     * they should both be of equal length, so it does not matter which is used.
     * @param xpoints The xpoints for this polygon.
     * @param ypoints The ypoints for this polygon.
     */
    public FlipPolygon(int[] xpoints, int[] ypoints)
    {
        this(xpoints, ypoints, xpoints.length);
    }

    /**
     * Creates a new FlipPolygon which will contain the given points.
     * The length of xpoints and ypoints must be equal to npoints.
     * @param xpoints The X-axis points for this polygon.
     * @param ypoints The Y-axis points for this polygon.
     * @param npoints The number of points stored in this polygon.
     */
    public FlipPolygon(int[] xpoints, int[] ypoints, int npoints)
    {
        super(xpoints, ypoints, npoints);
    }

    /**
     * @return A clone of this but with points flipped along the X axis.
     */
    public FlipPolygon flipX()
    {
        return new FlipPolygon(flip(xpoints), ArrayUtil.copyOf(ypoints, npoints), npoints);
    }

    /**
     * @return A clone of this but with points flipped along the Y axis.
     */
    public FlipPolygon flipY()
    {
        return new FlipPolygon(ArrayUtil.copyOf(xpoints, npoints), flip(ypoints), npoints);
    }

    /**
     * @return A copy of this only with points flipped on both the X and Y axis.
     */
    public FlipPolygon flipXY()
    {
        return new FlipPolygon(flip(xpoints), flip(ypoints), npoints);
    }

    /**
     * @param points
     * @return A copy of points only with all it's elements flipped around 0.
     */
    private int[] flip(int[] points)
    {
        final int[] newPoints = new int[points.length];
        for (int i = 0; i < points.length; i++) {
            newPoints[i] = -points[i];
        }
        return newPoints;
    }

    /**
     * @return A clone of this only with it's x and y points swapped.
     */
    public FlipPolygon swapXT()
    {
        return new FlipPolygon(ArrayUtil.copyOf(ypoints, npoints), ArrayUtil.copyOf(xpoints, npoints), npoints);
    }

    /**
     * @return A FlipPolygon with the same points as this one.
     * @throws java.lang.CloneNotSupportedException Will not be thrown by this implementation of the method.
     */
    @Override
    protected FlipPolygon clone() throws CloneNotSupportedException
    {
        return new FlipPolygon(
                ArrayUtil.copyOf(xpoints, npoints),
                ArrayUtil.copyOf(ypoints, npoints),
                npoints);
    }
}
